<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="过期时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'expire', validatorRules.expire]" placeholder="请输入过期时间" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="新人专享券" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'couponId', validatorRules.couponId]" placeholder="请输入新人专享券"></a-input>
        </a-form-item>
        <a-form-item label="新人完成订单之后的券包" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'couponPackageId', validatorRules.couponPackageId]" placeholder="请输入新人完成订单之后的券包"></a-input>
        </a-form-item>
        <a-form-item label="createTime" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择createTime" v-decorator="[ 'createTime', validatorRules.createTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="createBy" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'createBy', validatorRules.createBy]" placeholder="请输入createBy" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="updateTime" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择updateTime" v-decorator="[ 'updateTime', validatorRules.updateTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="updateBy" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'updateBy', validatorRules.updateBy]" placeholder="请输入updateBy" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="deleted" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'deleted', validatorRules.deleted]" placeholder="请输入deleted" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="活动状态，1启用，0关闭" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'state', validatorRules.state]" placeholder="请输入活动状态，1启用，0关闭" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="分享标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'shareTitle', validatorRules.shareTitle]" placeholder="请输入分享标题"></a-input>
        </a-form-item>
        <a-form-item label="shareImg" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'shareImg', validatorRules.shareImg]" placeholder="请输入shareImg"></a-input>
        </a-form-item>
        <a-form-item label="promotionId" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'promotionId', validatorRules.promotionId]" placeholder="请输入promotionId"></a-input>
        </a-form-item>
        
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'  
  
  export default {
    name: "NewUserActivityModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        expire:{rules: [{ required: true, message: '请输入过期时间!' }]},
        couponId:{},
        couponPackageId:{},
        createTime:{rules: [{ required: true, message: '请输入createTime!' }]},
        createBy:{},
        updateTime:{},
        updateBy:{},
        deleted:{rules: [{ required: true, message: '请输入deleted!' }]},
        state:{rules: [{ required: true, message: '请输入活动状态，1启用，0关闭!' }]},
        shareTitle:{rules: [{ required: true, message: '请输入分享标题!' }]},
        shareImg:{rules: [{ required: true, message: '请输入shareImg!' }]},
        promotionId:{},
        },
        url: {
          add: "/product/newUserActivity/add",
          edit: "/product/newUserActivity/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'expire','couponId','couponPackageId','createTime','createBy','updateTime','updateBy','deleted','state','shareTitle','shareImg','promotionId'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'expire','couponId','couponPackageId','createTime','createBy','updateTime','updateBy','deleted','state','shareTitle','shareImg','promotionId'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>